const app = getApp()

Page({
  data: {
    faceUrl: "../resource/images/noneface.png",
    

  },

  onLoad: function (params) {
    var me=this;

    var serverUrl = app.serverUrl;

    var user=app.userInfo;


    wx.showLoading({
      title: '请稍等...',
    });

    //调用后台接口
    wx.request({
      url: serverUrl + '/user/queryUserInfo?userId=' + user.id,
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        wx.hideLoading();
        if (res.data.status == 200) {
          var userInfo = res.data.data;
          var faceUrl = serverUrl + userInfo.faceImage;
          me.setData({
            faceUrl: faceUrl,
            fansCounts: userInfo.fansCounts,
            followCounts: userInfo.followCounts,
            receiveLikeCounts: userInfo.receiveLikeCounts,
            nickname: userInfo.nickname
          });
         
        }

      }
    })
   
  },

  logout:function(params) {
    var user=app.userInfo;

    var serverUrl = app.serverUrl;
    console.log(user.id);

    wx.showLoading({
      title: '请稍等...',
    });

    //调用后台接口
    wx.request({
      url: serverUrl+'/logout?userId='+user.id,
      method:"POST",
      header:{
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        console.log(res.data);
        wx.hideLoading();
        if (res.data.status == 200) {
          app.userInfo=null;
          wx.showToast({
            title: '注销成功',
            icon:'success',
            duration:2000
          });
          wx.navigateTo({
            url: '../userLogin/login',
          })
        }

      }
    })
  },
  changeFace:function(){
    var me=this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.showLoading({
          title: '上传中...',
        })
        var serverUrl = app.serverUrl;
        wx.uploadFile({
          url: serverUrl +'/user/uploadFace?userId='+app.userInfo.id,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success:function(res){
            var data=JSON.parse(res.data);
            console.log(data);
            wx.hideLoading();
            if (data.status==200){
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              });
              var imgUrl=data.data;
              me.setData({
                faceUrl: serverUrl+imgUrl
              });
            } else if (data.status == 500){
              wx.showToast({
                title: data.msg
              })
            }
          }
        })

      }
    })
  },
  
  uploadVideo:function(){
    var me=this;
    wx.chooseVideo({
      sourceType: ['album'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        console.log(res)
        var duration = res.duration;
        var tmpHeight = res.height;
        var tmpWidth = res.width;
        var tmpVideoUrl = res.tempFilePath;
        var tmpCoverUrl = res.thumbTempFilePath;

        if (duration>16){
          wx.showToast({
            title: '视频长度不能超过10秒...',
            icon:'none',
            duration:2500
          })
         }else{
           //TODO打开选择BGM页面
           wx.navigateTo({
             url: '../chooseBgm/chooseBgm?duration=' + duration 
               + "&tmpHeight=" + tmpHeight
               + "&tmpWidth=" + tmpWidth
               + "&tmpVideoUrl=" + tmpVideoUrl
               + "&tmpCoverUrl=" + tmpCoverUrl
           })
         }

      }
    })
  }


})
